package com.lagou.client;

import com.lagou.service.UserService;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.List;

public class ClientBootStrap {

    public static final String providerName = "UserService#sayHello#";
    public static List<String> children;
    public static UserService proxy;

    public static ZkClient zkClient = new ZkClient("127.0.0.1:2181");

    public static void main(String[] args) throws Exception {
        try {
            RpcConsumer rpcConsumer = new RpcConsumer();
            children = zkClient.getChildren("/127.0.0.1");

            zkClient.subscribeChildChanges("/127.0.0.1", new IZkChildListener() {
                @Override
                public void handleChildChange(String s, List<String> list) throws Exception {
                    System.out.println("子节点变化");
                    RpcConsumer rpcConsumer = new RpcConsumer();
                    List<String> childrens = zkClient.getChildren("/127.0.0.1");
                    System.out.println(childrens);
                    System.out.println(123);
                    System.out.println(456);
                    System.out.println(rpcConsumer.toString());
                    proxy = (UserService) rpcConsumer.createProxy(UserService.class, providerName, childrens,true,false);
//                    System.out.println(proxy);
                    trys();
                }
            });



            proxy = (UserService) rpcConsumer.createProxy(UserService.class, providerName, children,false,false);
            while (true) {
                proxy = (UserService) rpcConsumer.createProxy(UserService.class, providerName, children,false,true);
                Thread.sleep(2000);
                String s = proxy.sayHello("are you ok?");
                System.out.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static void trys() throws InterruptedException {
        while (true) {
            Thread.sleep(2000);
            System.out.println(proxy.sayHello("are you ok?"));
        }
    }





}
